Skip to content

Add http.route attribute and update span name from Plug router#621

Open
lunks wants to merge 5 commits intoopen-telemetry:mainfrom
lunks:fix/bandit-update-span-name-with-http-route
Open

Add http.route attribute and update span name from Plug router#621
lunks wants to merge 5 commits intoopen-telemetry:mainfrom
lunks:fix/bandit-update-span-name-with-http-route

Conversation

@lunks
Copy link
Copy Markdown

@lunks lunks commented Mar 6, 2026

Summary

  • At request stop, reads conn.private[:plug_route] to build "GET /items/:id" span names and set the http.route attribute
  • Non-standard HTTP methods are normalized to "HTTP" to prevent unbounded span name cardinality
  • Works with Phoenix and any Plug-based router that sets :plug_route

Follows up on #401.

Bandit created HTTP server spans with atom names (e.g. :GET) from semconv
values. The OTel spec requires span names to be strings. This changes
the initial span name to use conn.method (a string like "GET").

Additionally, at request completion, the span name is updated to include
the route template ("{method} {route}") when conn.private[:plug_route]
is available. This is set by Phoenix and other Plug-based routers during
dispatch, enabling proper span names without requiring opentelemetry_phoenix.

Follows up on open-telemetry#401 and the discussion about OTel HTTP semantic conventions
requiring span names to be "{method} {http.route}" when the route is known.
@lunks lunks requested a review from a team as a code owner March 6, 2026 06:33
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Mar 6, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 15a197cb52

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread instrumentation/opentelemetry_bandit/lib/opentelemetry_bandit.ex
Comment thread instrumentation/opentelemetry_bandit/lib/opentelemetry_bandit.ex Outdated
Avoid unbounded cardinality by mapping non-standard HTTP methods to
"HTTP" in maybe_update_span_name, matching the initial span name logic.

The exception handler cannot update the span name with the route because
Bandit's exception telemetry uses the original conn from before the Plug
handler ran, so conn.private[:plug_route] is not available.
@lunks
Copy link
Copy Markdown
Author

lunks commented Mar 6, 2026

Addressed the review feedback in c0df875:

Re: exception handler span naming — Bandit's exception telemetry uses the original conn from before the Plug handler ran (pipeline.ex captures conn at span start, before call_plug!). So conn.private[:plug_route] is not available in the exception path. This is a Bandit limitation — the exception handler can't know the route.

Re: unbounded cardinality from unknown methods — Added sanitize_method/1 that maps non-standard HTTP methods to "HTTP" in maybe_update_span_name, matching the initial span name logic.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c0df875ea9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread instrumentation/opentelemetry_bandit/lib/opentelemetry_bandit.ex
- Set HTTPAttributes.http_route() attribute when route is available
- Add test for POST method with route update
- Add test for 5xx response with route update
Comment thread instrumentation/opentelemetry_bandit/lib/opentelemetry_bandit.ex Outdated
@tsloughter
Copy link
Copy Markdown
Member

Why a string over an atom?

@lunks
Copy link
Copy Markdown
Author

lunks commented Mar 8, 2026

I can revert the atom-to-string change since it's not necessary for adding the route from plug_route, but the Otel spec states that span names should be strings. It's a good practice to follow, so you don't need to check the Otel library internals to see if it's a string or an atom, or add guards against both.

From my research, only the cowboy, bandit, and req libraries use atoms as span names. Ruby Otel libraries, although not enforced by the SDK, also use strings.

@yordis
Copy link
Copy Markdown
Member

yordis commented Mar 8, 2026

@lunks I do not disagree with the inconsistency, but I would rather keep the PR focused on one thing at a time. We need to clean up, and we need to first document them so we are all aligned and know what to expect.

@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@lunks lunks changed the title Use string span names and update with route from Plug router Add http.route attribute and update span name from Plug router Mar 10, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 98285aea28

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread instrumentation/opentelemetry_bandit/lib/opentelemetry_bandit.ex
|> Tracer.set_attributes()
end

maybe_update_span_name(conn)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This couldn't be moved to the start event?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. We only have the URL path, not the route name. call_plug! sets it, but the span has already been started:
https://github.com/mtrudel/bandit/blob/1.10.3/lib/bandit/pipeline.ex#L38-L42

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsloughter I am gonna defer to you, I am not sure if changing midway the span name is a major concern or not

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you folks think there's a better approach to accomplish a similar result, let me know.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. This is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants